<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <script>
        //【1】函数名作为其他函数的参数
        function callSomeFunction(someFunction, someArgument) {
            return someFunction(someArgument);
        }

        function add10(num) {
            return num + 10;
        }
        var result1 = callSomeFunction(add10, 10);
        console.log(result1); //20

        function getGreeting(name) {
            return "hello " + name;
        }
        var result2 = callSomeFunction(getGreeting, "sky");
        console.log(result2); //hello sky
        //【2】函数作为参数，内部返回值是一个函数

        var data = [{
            name: "Zachary",
            age: 38
        }, {
            name: "Zachary",
            age: 28
        }, {
            name: "Nicholas",
            age: 29
        }];
        data.sort(createComparisonFunction("age"));
        //将对象的某个属性进行排序
        function createComparisonFunction(propertyName) {
            return function(object1, object2) {
                var value1 = object1[propertyName];
                var value2 = object2[propertyName];
                if (value1 < value2) {
                    return -1;
                } else if (value1 > value2) {
                    return 1; //只要是首参比较2参，大于为1，即为升序
                } else {
                    return 0;
                }
            }
        }
        console.log(data); //
        console.log(data[0].age); //升序
    </script>
</body>

</html>